function [trainData, testData] = prepareData(dataFolder)
    trainData = loadDataFromFolder(fullfile(dataFolder, 'train'));
    testData = loadDataFromFolder(fullfile(dataFolder, 'test'));
    
    fprintf('已加载训练样本: %d\n测试样本: %d\n', ...
        length(trainData), length(testData));
    
    validateDataDimensions(trainData);
    validateDataDimensions(testData);
end

function data = loadDataFromFolder(folder)
    files = dir(fullfile(folder, '*.csv'));
    if isempty(files)
        error('文件夹中未找到CSV文件: %s', folder);
    end
    
    data = cell(1, length(files));
    for i = 1:length(files)
        try
            tableData = readtable(fullfile(folder, files(i).name));
            
            % 提取基础字段
            sample = struct();
            sample.acc = [tableData.acc_x, tableData.acc_y, tableData.acc_z];
            sample.vel = [tableData.vel_x, tableData.vel_y, tableData.vel_z];
            sample.gyro = [tableData.gyro_x, tableData.gyro_y, tableData.gyro_z];
            sample.pos = [tableData.pos_x, tableData.pos_y, tableData.pos_z];
            sample.fa = [tableData.fa_x, tableData.fa_y, tableData.fa_z];
            
            % 拼接完整特征矩阵（顺序与Python配置一致）
            sample.features = [sample.acc, sample.vel, sample.gyro, sample.pos];
            
            sample.condition = erase(files(i).name, '.csv');
            data{i} = sample;
        catch e
            warning('文件%s加载失败: %s', files(i).name, e.message);
        end
    end
    data = data(~cellfun('isempty', data));
end

function validateDataDimensions(data)
    for i = 1:length(data)
        fields = {'acc', 'vel', 'gyro', 'pos', 'fa', 'features'};
        for j = 1:length(fields)
            if ~isfield(data{i}, fields{j})
                error('样本%d缺少%s字段', i, fields{j});
            end
        end
        
        % 检查各字段维度
        if size(data{i}.features, 2) ~= 12 || ...
           any(cellfun(@(f) size(data{i}.(f), 2) ~= 3, {'acc', 'vel', 'gyro', 'pos', 'fa'}))
            error('样本%d数据维度异常', i);
        end
    end
end
